package cn.zh.npro.Adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Activity.EditClient;
import cn.zh.npro.Item.CaItem;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/3/7 0007.
 */

public class CaAdapter extends BaseAdapter{
    Context context;
    ArrayList<CaItem> arrayList;
    LayoutInflater inflater;
    String clienId="";
    Dialog dialog;
    public CaAdapter(Context context,ArrayList<CaItem>arrayList,String clienId){
        this.context=context;
        this.arrayList=arrayList;
        this.clienId=clienId;
        inflater=LayoutInflater.from(context);
       }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder holder=null;
        if (null==view){
            holder=new Holder();
            view=inflater.inflate(R.layout.ca_layut,null,false);
            holder.cpname=view.findViewById(R.id.cpname);
            holder.cpmobile=view.findViewById(R.id.cpmobile);
            holder.dename=view.findViewById(R.id.dename);
            holder.cpdress=view.findViewById(R.id.cpdress);
            holder.ratxt=view.findViewById(R.id.ratxt);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.text_select_color));
            holder.cpedit=view.findViewById(R.id.cpedit);
            holder.cpdete=view.findViewById(R.id.cpdete);
            holder.radio=view.findViewById(R.id.radio);
            view.setTag(holder);
          }else {
            holder= (Holder) view.getTag();
          }
        holder.cpname.setText(arrayList.get(i).getFullname());
        holder.cpmobile.setText(arrayList.get(i).getMobile());
        holder.cpdress.setText(arrayList.get(i).getAddress());
        if (arrayList.get(i).isMaster()){
            holder.radio.setBackgroundResource(R.mipmap.round_check_fill);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        }else {
            holder.radio.setBackgroundResource(R.mipmap.round);
            holder.ratxt.setTextColor(context.getResources().getColor(R.color.text_select_color));
         }

        holder.dename.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              if (arrayList.get(i).isMaster()){
              }else {
                  //设置默认联系人
                  getPostlink(arrayList.get(i).getLinkmanid(),i);
                 }
              }
        });

        holder.cpedit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,EditClient.class);
                intent.putExtra("type","0");
                intent.putExtra("ob",arrayList.get(i));
                context.startActivity(intent);
             }
          });

        holder.cpdete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog(i);
            }
        });

        return view;
         }



    public  class Holder{
      TextView cpname,cpmobile,cpdress,ratxt,cpedit,cpdete;
      ImageView radio;
      LinearLayout dename;
       }




    public void getDialog(final int i){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.out_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.out_layout);// 加载布局
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                getpost(arrayList.get(i).getLinkmanid(),i);
            }
        });
        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否删除此联系人?");
          }

    public void getpost(String id, final int pos){
        OkHttpUtils.post(BaseUrl.CustomerDelete)
                .params("customerId",id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------->删除客户==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                arrayList.remove(pos);
                                notifyDataSetChanged();
                            }else {
                                String Msg=js.getString("Msg");
                                ToastUtil.showLong(context,Msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                      }
                   });
                 }


             public void getPostlink(String linid, final int pos){
              OkHttpUtils.post(BaseUrl.LinkmanIsMaster)
                      .params("linkmanid",linid)
                      .params("customerId",clienId)
                      .execute(new StringCallback() {
                          @Override
                          public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                              try {
                                  JSONObject js=new JSONObject(s);
                                  boolean State=js.getBoolean("State");
                                  if (State){
                                    for (int i=0;i<arrayList.size();i++){
                                        if (i==pos){
                                            arrayList.get(i).setMaster(true);
                                        }else {
                                            arrayList.get(i).setMaster(false);
                                        }
                                    }
                                     notifyDataSetChanged();
                                  }else {
                                   String Msg=js.getString("Msg");
                                      ToastUtil.showLong(context,Msg);
                                  }
                              } catch (JSONException e) {
                                  e.printStackTrace();
                              }
                           }
                          @Override
                          public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                              super.onError(isFromCache, call, response, e);
                          }
                      });
                    }

          }
